package com.wsoft.hr.pm.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.hr.pm.entity.HrPmGradeMemberEntity;
import com.wsoft.hr.pm.entity.HrPmGradeOrgEntity;
import com.wsoft.hr.pm.manager.HrPmGradeMemberManager;
import com.wsoft.hr.pm.manager.HrPmGradeOrgManager;
import com.wsoft.hr.pm.mapper.HrPmGradeMemberMapper;
import com.wsoft.hr.pm.query.grade.member.HrPmGradeRemoveMemberInsertQuery;
import com.wsoft.hr.pm.query.grade.member.HrPmGradeRemoveMemberListQuery;
import com.wsoft.hr.pm.service.IHrPmGradeMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Z001
 * @Date 2025-03-01 08:35:23
 */
@Service
public class HrPmGradeMemberServiceImpl extends ServiceImpl<HrPmGradeMemberMapper, HrPmGradeMemberEntity> implements IHrPmGradeMemberService{

    @Resource
    private HrPmGradeMemberManager hrPmGradeMemberManager;
    @Resource
    private HrPmGradeOrgManager gradeOrgManager;

    /**
     * 新增-考核评级人员表
     * @param query
     * @return
     */
    @Override
    public boolean insertHrPmGradeMember(HrPmGradeRemoveMemberInsertQuery query) {
        List<Long> updateId = new ArrayList<>();
        for (Long id : query.getIds()) {
            HrPmGradeMemberEntity entity = hrPmGradeMemberManager.getById(id);
            List<Long> ids = hrPmGradeMemberManager.getIds(entity.getGradeId(), entity.getMemberId(),1);
            updateId.addAll(ids);
        }
        return hrPmGradeMemberManager.updateIsRemove(false,updateId);
    }

    /**
     * 删除-考核评级人员表
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        HrPmGradeMemberEntity entity = hrPmGradeMemberManager.getById(id);
        List<Long> ids = hrPmGradeMemberManager.getIds(entity.getGradeId(), entity.getMemberId(),0);
        if(CollUtil.isEmpty(ids)){
            return true;
        }
        return hrPmGradeMemberManager.updateIsRemove(true, ids);
    }

    /**
     * 移除人员列表
     * @param query
     * @return
     */
    @Override
    public PageInfo<HrPmGradeMemberEntity> removeMemberList(HrPmGradeRemoveMemberListQuery query) {
        HrPmGradeOrgEntity orgEntity = gradeOrgManager.getById(query.getId());
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        query.setGradeId(orgEntity.getGradeId());
        List<HrPmGradeMemberEntity> list = hrPmGradeMemberManager.removeMemberList(query);
        return PageInfo.of(list);
    }
}
